import { Injectable } from '@angular/core';
import { BaseService } from '@core/routes-services/base.service';
import { CacheService } from '@core/routes-services/cache.service';
import { HttpClient } from '@angular/common/http';
import { formatDate } from '@core/util/date-utils';

/**
 * 商品 服务
 */
@Injectable()
export class CompanyService extends BaseService {

  step: 0 | 1 | 2 | 3 = 0;
  entity: any = { id: 0 };

  imgList: any [] = [];
  profitCusList: any [] = [];
  profitSetting: any = null;

  reset() {
    this.step = 0;
    this.entity = { id: 0 };
    this.imgList = [];
    this.profitCusList = [];
    this.profitSetting = null;
  }

  constructor(http: HttpClient, cacheService: CacheService) {
    super(http, cacheService);
  }

  base_url: any = 'api/company';


  /**
   * 分页
   *
   * @param user
   * @returns {Promise<any>}
   */
  pagination(page: any, keyword: any): Promise<any> {
    const url = this.base_url + '/pagination';
    if (keyword) {
      page['_filter_like_title'] = keyword;
    }
    page['_sort_by_'] = 'sortNum_desc';
    return this.post(url, page);
  }

  /**
   * 获取所有列表 按排序号 升序排序
   * @returns {Promise<any>}
   */
  getAll(): Promise<any> {
    const url = this.base_url + '/all';
    return this.get(url);
  }
  /**
   * 修改
   *
   * @param id
   * @param entity
   * @returns {Promise<any>}
   */
  saveEntity(entity: any): Promise<any> {
    const url = this.base_url;
    if (entity.activityProductArr) {
      entity.activityProductArr = '[' + entity.activityProductArr.toString() + ']';
    }
    if (entity.beginTime) {
      entity.beginTime = formatDate(entity.beginTime, 'yyyy-MM-dd 00:00:00');
    }
    if (entity.endTime) {
      entity.endTime = formatDate(entity.endTime, 'yyyy-MM-dd 23:59:59');
    }
    return this.post(url, entity);
  }

  /**
   * 更新商品基础信息
   *
   * @param id
   * @param entity
   * @returns {Promise<any>}
   */
  updateEntity(id: any, entity: any): Promise<any> {
    if (entity.activityProductArr) {
      entity.activityProductArr = '[' + entity.activityProductArr.toString() + ']';
    }
    console.log(entity);
    if (entity.beginTime) {
      entity.beginTime = formatDate(entity.beginTime, 'yyyy-MM-dd 00:00:00');
    }
    if (entity.endTime) {
      entity.endTime = formatDate(entity.endTime, 'yyyy-MM-dd 23:59:59');
    }
    const url = this.base_url + '/' + id;
    return this.put(url, entity);
  }

  /**
   * 更新活动轮播图
   *
   * @param id
   * @param entity
   * @returns {Promise<any>}
   */
  updateImgs(id: any, entity: any): Promise<any> {
    const url = this.base_url + '/imgs/' + id;
    return this.put(url, entity);
  }


  /**
   * 活动积分奖励比率
   *
   * @param id
   * @param entity
   * @returns {Promise<any>}
   */
  updatePrices(id: any, entity: any): Promise<any> {
    const url = this.base_url + '/ratio/' + id;
    return this.put(url, entity);
  }

  /**
   * 获取查看详情
   *
   * @param id
   * @returns {Promise<any>}
   */
  detail(id: any): Promise<any> {
    const url = this.base_url + '/' + id;
    return this.get(url);
  }

  setInActivity(id: any, entity: any): Promise<any> {
    const url = this.base_url + '/setInActivity/' + id;
    return this.put(url, entity);
  }

  setUnActivity(id: any, entity: any): Promise<any> {
    const url = this.base_url + '/setUnActivity/' + id;
    return this.put(url, entity);
  }

  /**
   *交换排序号
   * @param fromId
   * @param toId
   * @returns {Promise<any>}
   */
  exchangeSortNum(fromId: any, toId: any): Promise<any> {
    console.log(fromId +'==='+toId);
    const url = this.base_url + '/actions/exchange/sortNum/' + fromId + '/' + toId;
    return this.put(url, {});
  }

  /**
   *设置置顶序号
   * @param newId
   * @param entity
   * @returns {Promise<any>}
   */
  setTopNum(newId: any, entity: any): Promise<any> {
    const url = this.base_url + '/actions/topNum/' + newId;
    return this.put(url, entity);
  }

  /**
   * 交换置顶排序号
   * @param fromId
   * @param toId
   * @returns {Promise<any>}
   */
  exchangeTopNum(fromId: any, toId: any): Promise<any> {
    const url = this.base_url + '/actions/exchange/topNum/' + fromId + '/' + toId;
    return this.put(url, {});
  }

}
